// 若不需要考虑兼容性可以不包含babel-polyfill，仅需在入口文件包含
import "babel-polyfill";
import "./main.scss";
import "utils/interceptors";
import Vue from "vue";
import Vuex from "vuex";
import VueRouter from "vue-router";
import store from "./store";
import routes from "./routes";
import AppMain from "comp/layout/Main.vue";
import runtime from "utils/runtime";

// 引入ui组件
import ElementUI from 'element-ui';
import 'theme/index.css';

import navItem from 'comp/packages/navItem';
navItem.install(Vue);


Vue.use(ElementUI);



Vue.use(VueRouter);

Vue.use(Vuex);

//Vue.config.devtools = true;

//mock数据
import mockData from "./mockData";
// Vue.config.devtools = true;

const app = new Vue({
    components: {AppMain},
    store: new Vuex.Store(store),
    router: new VueRouter(routes),
    methods:{
        /* 路由变化时处理右侧面包屑导航 */
        changeRouter(){
            let curPath = this.$route.path;
            let curNav = runtime.matchCurNav(curPath, this.$store.state.common.subNavList);
            curNav && this.$store.commit("common.addCrumbTag", curNav);
            this.$store.commit("common.modifyCurNav", curPath);
        }
    },
    created() {
        /* 路由变化时处理右侧面包屑导航 */
        this.$router.afterEach((to,from, next)=>{
            this.changeRouter();
        });

        this.$store.dispatch("common.user")
            .then(() => {
                this.$store.dispatch("common.navList")
                    .then(data => {
                        console.log(data);
                    })
                    .catch(msg => {
                        console.error(msg);
                    })
            })
            .catch(() => {
                alert('未登录,请先登录！');
            })

        this.$store.dispatch("common.subNavList")
            .then(data => {
                this.changeRouter();
                this.$store.commit("common.modifyCurNav", this.$route.path);
                console.log('subNavList data', data);
            })
            .catch(msg => {
                console.error('subNavList error', msg);
            })
    }
}).$mount("#app");
